﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using System.IO;

namespace Server
{
    public partial class Main : Form
    {
        Editon Edit = new Editon();
        string Path = System.IO.Directory.GetCurrentDirectory() + "\\menu.txt";

        public Main()
        {
            InitializeComponent();
            
            Thread Lisent_t = new Thread(new ThreadStart(Lisent));
            Thread FreshList_th = new Thread(new ThreadStart(FreshList));
            FreshList_th.Start();
            Lisent_t.Start();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Edit.ShowDialog();
        }


              

        void FreshList()
        {
            while(true)
            {
                try
                {
                    string[] Menu_All = File.ReadAllLines(Path);
                    string temp = "";
                    for (int i = 0; i < Menu_All.Length; i++)
                    {
                        temp += Menu_All[i] + "\r\n";
                        Invoke((EventHandler)delegate {
                            textBox1.Text = temp;
                        });
                    }
                }
                catch
                {

                }
                
                Thread.Sleep(100);
            }
        }

        void Lisent()
        {
            while (true)
            {
                int recv;
                byte[] data = new byte[1024];
                IPEndPoint ipep = new IPEndPoint(IPAddress.Any,9050);
                Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                newsock.Bind(ipep);
                newsock.Listen(10);
                //textBox1.Text += "waiting for a client\r\n";
                Socket client = newsock.Accept();
                IPEndPoint clientip = (IPEndPoint)client.RemoteEndPoint;
                //textBox1.Text += "connect with client: " + clientip.Address + " at port: " + clientip.Port + "\r\n";
                //textBox1.Text += "Welcome Here\r\n";
                string welcome = "测试";
                data = Encoding.ASCII.GetBytes(welcome);
                //lisent.Send(data, data.Length, SocketFlags.None); // 发送信息
                while (true)
                { // 用死循环来不断的从客户端获取信息 
                    data = new byte[1024];
                    recv = client.Receive(data);
                    //textBox1.Text += " recv= " + recv;
                    if (recv == 0) // 当信息长度为0，说明客户端连接断开 
                        break;
                    Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
                    client.Send(data, recv, SocketFlags.None);
                }
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void bt_Show_Server_Click(object sender, EventArgs e)
        {
            MessageBox.Show("服务器地址为："+GetLocaIPV4()+"   端口为：9050");
        }

        string GetLocaIPV4()
        {
            try
            {
                string HostName = Dns.GetHostName(); //得到主机名
                IPHostEntry IpEntry = Dns.GetHostEntry(HostName);
                for (int i = 0; i < IpEntry.AddressList.Length; i++)
                {
                    if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
                    {
                        return IpEntry.AddressList[i].ToString();
                    }
                }
                return "";
            }
            catch (Exception ex)
            {
                MessageBox.Show("获取本机IP出错:" + ex.Message);
                return "";
            }
        }

        private void lb_Menu_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
